package com.icss.struct.action;

import com.icss.struct.dao.UserDao;
import com.icss.struct.dto.R;
import com.icss.struct.dto.UserInfo;

import java.util.HashMap;

public class UserAction {
    public Object login(HashMap<String, String> params) {
        System.out.println("--------UserAction@login-----------");
//        params.forEach((k,v) -> System.out.println(k + v));
        UserDao dao = new UserDao();
        UserInfo pojo = dao.findUserInfoByUsername(params.get("username"));
        R r = new R();
        if (pojo == null) {
            r.setCode(20000);
            r.setMessage("用户名不存在");
        } else {
            if (!params.get("password").equals(pojo.getPassword())) {
                r.setCode(20000);
                r.setMessage("密码错误");
            } else {
                r.setCode(20000);
                r.setMessage("success");
            }
        }

        return r;
    }

    /**
     * 注册功能     *
     * @param params
     * @return
     */
    public Object regist(HashMap<String, String> params) {
        System.out.println("--------UserAction@regist-----------");
        UserDao dao = new UserDao();
        dao.addUser(params);
        R r = new R();
        r.setCode(20000);
        r.setMessage("注册成功");
        return r;
    }

    public Object checkname(HashMap<String, String> params) {
        System.out.println("--------UserAction@checkname-----------");
        UserDao dao = new UserDao();
        UserInfo pojo = dao.findUserInfoByUsername(params.get("username"));
        R r = new R();
        if (pojo == null) {
            r.setCode(20000);
            r.setMessage("用户名未被注册");
        } else {
            r.setCode(20000);
            r.setMessage("用户名已被注册");
        }
        return r;
    }
}
